package com.msk.common.utils;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.UUID;

/**
 * @Description 随机验证码工具类 一个集成自 java.util.Random 增加另一个方法,为获取一个区间的随机数 例如：可以获得 10-20之间的一个随机数
 * @Author zlf
 * @Date 2023/5/9 17:06
 */
public class RandomUtil extends Random {
	// Suppress default constructor for non-instantiability
	private RandomUtil() {
		throw new AssertionError();
	}

	private static final long serialVersionUID = 1L;

	/**
	 * 返回一个伪随机数，它是取自此随机数生成器序列的、在 start 和 end 之间均匀分布的 int 值。 nextInt
	 * 的常规协定是，伪随机地生成并返回指定范围中的一个 int 值。所有可能的 n 个 int 值的生成概率（大致）相同 。Random
	 * 类按如下方式实现 nextInt(int n) 方法： Random random = new Random(); while(true){
	 * int number = random.nextInt(end+1); if(number>=start){ return number; } }
	 *
	 * @param start
	 *            区间开始值
	 * @param end
	 *            区间结束值
	 * @return 下一个伪随机数，在此随机数生成器序列中 在 start 和 end 之间均匀分布的 int 值。
	 */
	public static int interval(int start, int end) {

		RandomUtil random = new RandomUtil();
		while (true) {
			int number = random.nextInt(end + 1);
			if (number >= start) {
				return number;
			}
		}
	}

	/***
	 * 随机产生四位数 内部调用的是interval(int start,int end)
	 *
	 * @return 返回生成的伪随机数
	 */
	public static String four() {
		return new Integer(interval(1000, 9999)).toString();
	}

	/***
	 * 随机产生三位数 内部调用的是interval(int start,int end)
	 *
	 * @return 返回生成的伪随机数
	 */
	public static int three() {
		return interval(100, 999);
	}

	/***
	 * 随机产生三位数 内部调用的是interval(int start,int end)
	 *
	 * @return 返回生成的伪随机数
	 */
	public static String six() {
		return new Integer(interval(100000, 999999)).toString();
	}

	public static final String numberChar = "0123456789";

	/**
	 * 随机短信验证码
	 */
	public static String prduceRand() {
		int[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		RandomUtil rand = new RandomUtil();
		for (int i = 10; i > 1; i--) {
			int index = rand.nextInt(i);
			int tmp = array[index];
			array[index] = array[i - 1];
			array[i - 1] = tmp;
		}
		StringBuffer str = new StringBuffer();
		for (int i = 0; i < 6; i++) {
			str.append(array[i]);
		}
		return str.toString();
	}

	/**
	 * 生成八位随机授权码
	 */
	public static String produceRandomNum(int bit) {
		Long seed = System.currentTimeMillis();// 获得系统时间，作为生成随机数的种子
		// 装载生成的随机数
		Random random = new Random(seed);// 调用种子生成随机数
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < bit; i++) {
			sb.append(numberChar.charAt(random.nextInt(numberChar.length())));
		}
		return sb.toString();
	}

	/**
	 * 阿里云code
	 * @return
	 */
	public static String aliCode(){

		String str="0123456789";
		String uuid=new String();
		for(int i=0;i<4;i++){
			char ch=str.charAt(new Random().nextInt(str.length()));
			uuid+=ch;
		}
		return uuid;
	}

	/**
	 * 阿里云首字母不为零code
	 * @return
	 */
	public static String aliCodeNew(){
		String str="0123456789";
		String uuid=new String();
		String firstLetter="123456789";
		char firstCh = firstLetter.charAt(new Random().nextInt(firstLetter.length()));
		uuid += firstCh;
		for(int i=1;i<4;i++){
			char ch= str.charAt(new Random().nextInt(str.length()));
			uuid+=ch;
		}
		return uuid;
	}

	/**
	 * 生成6位交易码，银行子账号使用
	 * @return
	 */
	public static String subAccountSeqCode(){

		String str="0123456789";
		String uuid=new String();
		for(int i=0;i<6;i++){
			char ch=str.charAt(new Random().nextInt(str.length()));
			uuid+=ch;
		}
		return uuid;
	}

	/**
	 * 生成指定位数随机数
	 * @param len
	 * @return
	 */
	public static String getCode(int len) {
		int[] array = {0,1,2,3,4,5,6,7,8,9};
		Random rand = new Random();
		for (int i = 10; i > 1; i--) {
			int index = rand.nextInt(i);
			int tmp = array[index];
			array[index] = array[i - 1];
			array[i - 1] = tmp;
		}
		StringBuilder result = new StringBuilder();
		for(int i = 0; i < len; i++) {
			result.append(array[i]);
		}
		return result.toString();
	}

	/**
	 * 随机码
	 * @return
	 */
	public static String getUUID(){
		String s = UUID.randomUUID().toString();
		//去掉“-”符号
		return s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24);
	}


	public static String getUniqueString(){
		Set<String> set = new HashSet<String>();
		while (true){
			Double random = Math.random();
			String str = random.toString().substring(2, 12);
			if(set.add(str))
				return str;
		}
	}

	/**
	 * 随机生成13位数字
	 * @return
	 */
	public static String get13Code(){
		String num = "";
		Random rand = new Random();
		num = (rand.nextInt(9) +1) + "";
		num = num + getCode(2);
		num = num +getUniqueString();
		return num;
	}

	public static char [] str = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
			'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
			'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

	public static char [] strCode = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
			'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
			'X', 'Y', '3', '4', '5', '6', '7', '8', '9' };
	/**
	 * Java生成8位随机邀请码,不重复
	 * @Author: wlxiang
	 * @Date: 2021/8/23 9:39
	 * @return: java.lang.String
	 **/
	public static String genRandomNum(){
		int  maxNum = 36;
		int i;
		int count = 0;

		StringBuffer pwd = new StringBuffer("");
		Random r = new Random();
		while(count < 8){
			i = Math.abs(r.nextInt(maxNum));
			if (i >= 0 && i < str.length) {
				pwd.append(str[i]);
				count ++;
			}
		}
		return pwd.toString();
	}

	/**
	 * Java生成指定位随机邀请码,不重复
	 * @Author: zlf
	 * @Date: 2022/4/15 9:39
	 * @return: java.lang.String
	 **/
	public static String genRandomNum(int num){
		int maxNum = 30;
		int i;
		int count = 0;

		StringBuffer pwd = new StringBuffer("");
		Random r = new Random();
		while(count < num){
			i = Math.abs(r.nextInt(maxNum));
			if (i >= 0 && i < strCode.length) {
				if (!pwd.toString().contains(String.valueOf(strCode[i]))) {
					pwd.append(strCode[i]);
					count ++;
				}
			}
		}
		return pwd.toString();
	}

	public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
			"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
			"t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
			"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
			"W", "X", "Y", "Z" };

	/**
	 * 短8位UUID思想其实借鉴微博短域名的生成方式，
	 * @Author: wlxiang
	 * @Date: 2021/8/23 9:39
	 * @return: java.lang.String
	 **/

	public static String generateShortUuid() {
		StringBuffer shortBuffer = new StringBuffer();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		for (int i = 0; i < 8; i++) {
			String str = uuid.substring(i * 4, i * 4 + 4);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(chars[x % 0x3E]);
		}
		return shortBuffer.toString();
	}

}
